package dao

import (
	"gorm.io/gorm"
	"myserver/model"
	"myserver/param"
	"myserver/tool"
)

type HouseDao struct {
	*tool.MyOrm
}

func (d *HouseDao) GetHouse(house *model.House) *gorm.DB {
	res := d.Db.First(&house)
	return res
}

func (d *HouseDao) CreateHouse(house model.House) int64 {
	res := d.Db.Create(&house)
	return res.RowsAffected
}

func (d *HouseDao) DeleteHouse(house model.House) int64 {
	res := d.Db.Delete(&house)
	return res.RowsAffected
}

func (d *HouseDao) UpdateHouse(house model.House) int64 {
	res := d.Db.Updates(&house)
	return res.RowsAffected
}

func (d *HouseDao) QueryHouses(pageParam param.PageParam) param.ResponsePageParam {
	rs := param.ResponsePageParam{}
	rs.Data = []model.House{}
	db := d.Db.Model(&model.House{})
	if pageParam.SearchContent != "" {
		db = db.Where("house_name LIKE ?", "%"+pageParam.SearchContent+"%")
	}
	db.Order("ID ASC").Count(&rs.Total).Limit(pageParam.Limit).Offset(pageParam.Page).Find(&rs.Data)
	return rs
}
